/*
 * Powered By XY
 * Since 2020
 */

package com.xy.biz.rs.controller;

import java.util.Collection;
import java.util.List;

import com.xy.lang.domain.Page;
import com.xy.lang.domain.Result;
import com.xy.lang.validation.SaveChecks;
import com.xy.lang.validation.UpdateChecks;
import com.xy.lang.validation.annotation.Validatable;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import com.xy.biz.rs.domain.resp.GetMaterialResponse;
import com.xy.biz.rs.domain.req.GetMaterialRequest;
import com.xy.biz.rs.domain.req.ListMaterialRequest;
import com.xy.biz.rs.domain.req.SaveMaterialRequest;
import com.xy.biz.rs.domain.query.MaterialQuery;
import com.xy.biz.rs.domain.converter.MaterialConverter;
import com.xy.biz.rs.client.MaterialClient;
import com.xy.biz.rs.service.MaterialService;

/**
 *
 *
 * @author yzh
 * @date 2021-4-14
 */
@Slf4j
@RestController
@RequestMapping("/api/rs/material")
public class MaterialController implements MaterialClient {

	@Autowired
	private MaterialService materialService;

	@ApiOperation("物料管理新增")
	@Validatable(groups = {SaveChecks.class})
	@PostMapping(value = "/save")
	public Result<?> saveMaterial(@RequestBody SaveMaterialRequest request) {
		return materialService.saveMaterial(request);
	}

	@Deprecated
	@Validatable(groups = {SaveChecks.class})
	@PostMapping(value = "/save_all")
	public Result<?> saveAllMaterial(@RequestBody Collection<SaveMaterialRequest> collection) {
		materialService.saveAllMaterial(MaterialConverter.toBO(collection));
		return Result.success();
	}

	@ApiOperation("物料管理修改")
	@Validatable(groups = {UpdateChecks.class})
	@PostMapping(value = "/update")
	public Result<?> updateMaterial(@RequestBody SaveMaterialRequest request) {
		return materialService.updateMaterial(request);
	}

	@ApiOperation("物料管理删除")
	@Validatable
	@PostMapping(value = "/delete")
	public Result<?> deleteMaterial(@RequestBody GetMaterialRequest request) {
		materialService.deleteMaterial(request.getMaterialId());
		return Result.success();
	}

	@Deprecated
	@Validatable
	@PostMapping(value = "/get")
	@Override
	public Result<GetMaterialResponse> getMaterial(@RequestBody GetMaterialRequest request) {
		return Result.success(MaterialConverter.toResponse(materialService.getMaterial(request.getMaterialId())));
	}

	@Deprecated
	@PostMapping(value = "/list_by_ids")
	@Override
	public Result<List<GetMaterialResponse>> listMaterialById(@RequestBody Collection<Long> collection) {
		return Result.success(MaterialConverter.toResponse(materialService.listMaterialById(collection)));
	}

	@Deprecated
	@PostMapping(value = "/list")
	@Override
	public Result<List<GetMaterialResponse>> listMaterial(@RequestBody ListMaterialRequest request) {
		return Result.success(MaterialConverter.toResponse(materialService.listMaterial(MaterialConverter.toQuery(request))));
	}

	@Deprecated
	@PostMapping(value = "/count")
	@Override
	public Result<Integer> countMaterial(@RequestBody ListMaterialRequest request) {
		return Result.success(materialService.countMaterial(MaterialConverter.toQuery(request)));
	}

	@ApiOperation("物料管理分页")
	@PostMapping(value = "/page")
	@Override
	public Result<Page<GetMaterialResponse>> pageMaterial(@RequestBody ListMaterialRequest request) {
		MaterialQuery query = MaterialConverter.toQuery(request);
		List<GetMaterialResponse> list = MaterialConverter.toResponse(materialService.listMaterial(query));
		int count = materialService.countMaterial(query);
		return Result.success(new Page<>(list, count));
	}

}
